package com.nsapi.niceschoolapi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nsapi.niceschoolapi.common.base.PageData;
import com.nsapi.niceschoolapi.entity.*;
import com.nsapi.niceschoolapi.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class SchoolManageController {
    @Resource
    private SchoolManageService schoolManageService;

    @Resource
    private CourseService courseService;

    @Resource
    private ScheduleService scheduleService;

    @Resource
    private WeeksService weeksService;

    @Resource
    private TchCourseService tchCourseService;

    private PageData<SchoolManageVO> pageData = new PageData<SchoolManageVO>();

    //查询系部表全部信息
    @RequestMapping(value = "/schoolmanage/findalldpm")
    @ResponseBody
    public PageData<SchoolManageVO> findalldpm() {
        List<SchoolManageVO> schoolManageList = schoolManageService.findAllDpm(null);
        pageData.setData(schoolManageList);
        return pageData;
    }

    //级联系部查询对应专业
    @RequestMapping(value = "/schoolmanage/jlselmajor")
    @ResponseBody
    public PageData<SchoolManageVO> jlselmajor(Integer did) {
        SchoolManageVO schoolManageVO = new SchoolManageVO();
        schoolManageVO.setDid(did);
        List<SchoolManageVO> schoolManageList = schoolManageService.jlSelMajor(schoolManageVO);
        pageData.setData(schoolManageList);
        return pageData;
    }

    //级联专业查询对应年级
    @RequestMapping(value = "/schoolmanage/jlselgrade")
    @ResponseBody
    public PageData<SchoolManageVO> jlselgrade(Integer mid) {
        SchoolManageVO schoolManageVO = new SchoolManageVO();
        schoolManageVO.setMid(mid);
        List<SchoolManageVO> schoolManageList = schoolManageService.jlSelGrade(schoolManageVO);
        pageData.setData(schoolManageList);
        return pageData;
    }

    //级联年级查询班级
    @RequestMapping(value = "/tchcoursemanage/findtchcourseclassname")
    @ResponseBody
    public PageData<SchoolManageVO> findtchcourseclassname(Integer gid) {
        SchoolManageVO schoolManageVO = new SchoolManageVO();
        schoolManageVO.setGid(gid);
        List<SchoolManageVO> schoolManageList = schoolManageService.classinfo(schoolManageVO);
        pageData.setData(schoolManageList);
        return pageData;
    }

    //级联专业查询对应课目
    @RequestMapping(value = "/tchcoursemanage/findallcoursebymajorname")
    @ResponseBody
    public PageData<Coursel> findallcoursebymajorname(Integer mid) {
        PageData<Coursel> pageDataCourse = new PageData<Coursel>();
        Coursel course = new Coursel();
        course.setMid(mid);
        List<Coursel> courselList = courseService.selCourse(course);
        pageDataCourse.setData(courselList);
        return pageDataCourse;
    }

    //查询节段
    @RequestMapping(value = "/tchcoursemanage/findallschedule")
    @ResponseBody
    public PageData<ScheduleDB> findallschedule() {
        PageData<ScheduleDB> pageDataCourse = new PageData<ScheduleDB>();
        List<ScheduleDB> schedulelist = scheduleService.findAllSchedule(null);
        pageDataCourse.setData(schedulelist);
        return pageDataCourse;
    }

    //查询星期
    @RequestMapping(value = "/tchcoursemanage/findallweeks")
    @ResponseBody
    public PageData<WeeksDB> findallweeks() {
        PageData<WeeksDB> pageDataCourse = new PageData<WeeksDB>();
        List<WeeksDB> weeksList = weeksService.getWeeksList();
        pageDataCourse.setData(weeksList);
        return pageDataCourse;
    }

    //添加授课信息
    @RequestMapping(value = "/tchcoursemanage/addonetchcourse")
    @ResponseBody
        public  Boolean addonetchcourse(TchCourseVO tchCourseVO){
        boolean bool=false;
        int reuslt =tchCourseService.findTchCourseFlag(tchCourseVO);
        if (reuslt>0){
            bool=false;
        }else{
            int reuslt2 = tchCourseService.findTchCourseDanger(tchCourseVO);
            if (reuslt2>0){
                bool=false;
            }else{
                int reuslt3 =tchCourseService.addOneTchCourse(tchCourseVO);
                if (reuslt3>0){
                    int result4 =tchCourseService.addOneTchClass(tchCourseVO);
                    if (result4>0){
                        bool=true;
                    }else bool=false;
                }else bool=false;
            }
        }
    return bool;

    }
   //查看教师授课
    @RequestMapping(value = "/tchcoursemanage/findcoursebytch")
    @ResponseBody
    public PageData<TchCourseVO> findcoursebytch(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                 @RequestParam(value = "limit" , defaultValue = "10") Integer limit,
                                                 TchCourseVO tchCourseVO){
        PageHelper.startPage(page,limit);
        List<TchCourseVO> tchCourseVOS=tchCourseService.findCourseByTch(tchCourseVO);
        PageInfo pageInfo=new PageInfo(tchCourseVOS);
        PageData<TchCourseVO> pageData=new PageData<TchCourseVO>();
         pageData.setData(pageInfo.getList());
         pageData.setCount(pageInfo.getTotal());
        return pageData;
    }
    //删除教师授课及班级
    @RequestMapping(value = "/tchcoursemanage/deltchcourse")
    @ResponseBody
    public Boolean deltchcourse(TchCourseVO tchCourseVO){
        int reslut=tchCourseService.delTchCourse(tchCourseVO);
        if (reslut>0){
            return true;
        }else{
            return false;
        }
    }
}
